package com.yh.workbill.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yh.workbill.R;
import com.yh.workbill.databinding.ItemLeaselistinfoBinding;
import com.yh.workbill.model.UseCompany;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

public class LeaseListInfoAdapter extends RecyclerView.Adapter<LeaseListInfoAdapter.ViewHolder> {

    private List<UseCompany> data;
    private OnItemLongClick onItemLongClick;

    public void setOnItemLongClick(OnItemLongClick onItemLongClick) {
        this.onItemLongClick = onItemLongClick;
    }

    public LeaseListInfoAdapter(List<UseCompany> data) {
        this.data = data;
    }

    public void setData(List<UseCompany> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemLeaselistinfoBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_leaselistinfo, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        UseCompany useCompany = data.get(position);
        holder.binding.setName(useCompany.getWork_use_company());
        holder.itemView.setOnLongClickListener(v -> {
            if (onItemLongClick != null) {
                onItemLongClick.onItemClick(position, useCompany);
            }
            return true;
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ItemLeaselistinfoBinding binding;

        public ViewHolder(@NonNull ItemLeaselistinfoBinding itemView) {
            super(itemView.getRoot());
            this.binding = itemView;
        }
    }

    public interface OnItemLongClick {
        void onItemClick(int position, UseCompany data);
    }
}
